package com.windy.dailynews.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.windy.dailynews.DailyNewsApplication;

/**
 * Created by dingfeng on 2016/12/27.
 */
public class Utils {

    public static boolean isNetworkAvailable(Context ct) {
        ConnectivityManager connectivity = (ConnectivityManager) ct.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        }
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (NetworkInfo name : info) {
                if (NetworkInfo.State.CONNECTED == name.getState()) {
                    return true;
                }
            }
        }
        return false;
    }

    public static int dp2px(float dpValue) {
        final float scale = DailyNewsApplication.getApplication().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dp(float pxValue) {
        final float scale = DailyNewsApplication.getApplication().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int sp2px(float spValue) {
        final float fontScale = DailyNewsApplication.getApplication().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

}
